import configs from "../Configs";
import { aa } from "../frame/FrameCore";
import Net from "../frame/managers/NetMgr";
import { ChatBoxView } from "../view/ChatBoxView";
const prompt = configs.prompt;

class ChatMgr {

    private _user = [];
    private _assistant = [];
    private _other = [];

    public async requestChat(content: string,chat:ChatBoxView):Promise<string[]> {
        aa.global.isTyping = true;
        const contents: any[] = [];
        if (this._user.length > 0) {
            for (var i = 0; i < this._user.length; i++) {
                let userC = this._user[i];
                contents.push({ "role": "user", "content": userC })
                contents.push({ "role": "assistant", "content": this._assistant[i] })
            }
        }
        contents.push({ "role": "user", "content":  content });
        console.log("chat0==", chat)
        let res = await this.requestLLM(contents, prompt,chat);
        aa.global.isTyping = false;
        if(res){
            this.cacheChat(content,res);
            const length = res.length;
     
            const time = Math.max(1.5,Math.floor(length/42)*10);
        console.log("current",length,time)
     
        }
        // let json = [res,10];
        return [res,content];
    }
  
   
    jsonParse(type, text: string) {
        let res = []
        try {
            res = JSON.parse(text);
        } catch (e) {
            res[0] = text;
            res[1] = 50;
        }
        return res;
    }

    async requestLLM(contents: any[], prompt,chat:ChatBoxView) {
        contents.unshift({ "role": "system", "content": prompt })
        const url = aa.global.LLMurl;
        const apiKey = aa.global.LLMkey;
        let data = {
            "model":aa.global.LLMmodel ,
            "max_tokens": 1000,
            // "stream": true  // 启用流式响应
        }
        data["messages"] = contents;
        let res = await Net.sendGptRequest(url, data, apiKey,chat);
        console.log("res==",res)
        if(res){
            return res;
        }else{
            return null;
        }
    }

    clearCache() {
        this._user.length = 0;
        this._assistant.length = 0;
        this._other.length = 0;
    }
    cacheChat(user: string, assistant: string) {
        const totalLength = this.getTotalCacheLength();
        if (totalLength > configs.cacheLength) {
            this._user.shift()
            this._assistant.shift()
            // this._other.shift()
        } else {
            if (this._user.length > configs.cacheSize) {
                this._user.shift()
                this._assistant.shift()
            }
        }
        this._user.push(user);
        this._assistant.push(assistant)
    }
    getTotalCacheLength() {
        let length = 0;
        this._user.forEach((text) => {
            length += text.length;
        })
        this._assistant.forEach((text) => {
            length += text.length;
        })
  
        return length;
    }

}

const chatMgr = new ChatMgr;

export default chatMgr;